package com.icoolkj.ms.user.controller;

import com.icoolkj.ms.api.auth.bo.UserInfoInTokenBO;
import com.icoolkj.ms.api.rbac.feign.PermissionFeignClient;
import com.icoolkj.ms.api.user.vo.UserApiVO;
import com.icoolkj.ms.common.core.enums.ResponseEnum;
import com.icoolkj.ms.common.core.response.MapBean;
import com.icoolkj.ms.common.core.response.ServerResponseEntity;
import com.icoolkj.ms.common.security.AuthUserContext;
import com.icoolkj.ms.user.service.IUserService;
import com.icoolkj.ms.user.vo.UserSimpleInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author icoolkj
 * @version 1.0
 * @description 用户信息
 * @createDate 2025/03/02 20:45
 */
@RestController
public class UserInfoController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private PermissionFeignClient permissionFeignClient;

    @GetMapping("getUserInfo")
    public ServerResponseEntity<MapBean> getUserInfo() {
        MapBean mapBean = MapBean.create();
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        if (userInfoInTokenBO == null) {
            return ServerResponseEntity.fail(ResponseEnum.TOKEN_EXPIRE);
        }
        UserApiVO userApiVO = iUserService.getUserInfoByUserId(userInfoInTokenBO.getUserId());
        if (userApiVO == null) {
            return ServerResponseEntity.fail(ResponseEnum.USER_NOT_FOUND);
        }
        UserSimpleInfoVO userSimpleInfoVO = new UserSimpleInfoVO();
        userSimpleInfoVO.setNickName(userApiVO.getNickName());
        userSimpleInfoVO.setPic(userSimpleInfoVO.getPic());
        mapBean.withParameter("user", userSimpleInfoVO);
        List<String> permission = permissionFeignClient.getUserMenuPermissions(userInfoInTokenBO.getUserId(), 
                userInfoInTokenBO.getUserType());
        mapBean.withParameter("permission", permission);
        List<String> roles = permissionFeignClient.selectRoleKeyListByUserId(userInfoInTokenBO.getUserId(), 
                userInfoInTokenBO.getUserType());
        mapBean.withParameter("roles", roles);
        return ServerResponseEntity.success(mapBean);
    }

}